package com.timoon.manager.panel.product;

import java.util.List;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.ajax.markup.html.navigation.paging.AjaxPagingNavigator;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.PageableListView;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

import com.timoon.component.panel.jlayout.JLayouterPanel;
import com.timoon.manager.auth.BaseAuthPanel;
import com.timoon.sample.dao.model.ProductComment;

/**
 *<p>功能:商品评论管理</p>
 *<p></p>
 *<p>Copyright:Copyright(c)2013</p>
 *<p>Company:sky</p>
 *@author 付乐天
 *@version 1.0 */

public class ProductCommentsPanel extends BaseAuthPanel {
	
	@SpringBean(name="manager.productComment")
	private ProductCommentsService service;

	public ProductCommentsPanel(String id) {
		super(id);
		// TODO Auto-generated constructor stub
		
		renderPanel();
	}

	private void renderPanel() {
		// TODO Auto-generated method stub
		List<ProductComment> comments=service.getAllProductComments();
		
		PageableListView<ProductComment> listView=new PageableListView<ProductComment>("wk_list",comments,30) {
			
			@Override
			protected void populateItem(ListItem<ProductComment> arg0) {
				// TODO Auto-generated method stub
				
				ProductComment comment=arg0.getModelObject();
				
				renderLabel("wk_id",comment,"id",arg0);
				renderLabel("wk_crete_date",comment,"createDate",arg0);
				renderLabel("wk_level",comment,"level",arg0);
				renderLabel("wk_advantage",comment,"advantage",arg0);
				renderLabel("wk_disadvantage",comment,"disadvantage",arg0);
				
				String publiced=null;
				String oparetor="";
				Integer toPubliced=0;
				if(comment.getPubliced().compareTo(new Integer(0))==0){
					publiced="公开";
					oparetor="隐藏";
					toPubliced=1;
				}else{
					publiced="隐藏";
					oparetor="公开";
					toPubliced=0;
				}
				arg0.add(new Label("wk_publiced",publiced));
				
				final Integer t=toPubliced;
				final ProductComment tcomment=comment;
			    AjaxLink link=new AjaxLink("wk_show") {

					@Override
					public void onClick(AjaxRequestTarget arg0) {
						// TODO Auto-generated method stub
						tcomment.setPubliced(t);
						
						service.updateProductComment(tcomment);
						
						JLayouterPanel layouter=(JLayouterPanel) this.getSession().getAttribute("layouter");
						
						layouter.updatePanel(JLayouterPanel.CENTER_PANEL, new ProductCommentsPanel(JLayouterPanel.CENTER_PANEL), arg0);
					}
				};
				link.add(new Label("wk_show_label",oparetor));
				arg0.add(link);
			}
		};
		add(listView);
		
		add(new AjaxPagingNavigator("wk_paging", listView));
	}
	
	private void renderLabel(String markupId,Object obj,String exe,ListItem<ProductComment> listView){
		listView.add(new Label(markupId,new PropertyModel<>(obj, exe)));
	}

}
